<template>
  <el-container>
    <el-header id="header" class="navbar navbar-inverse navbar-fixed-top">
      <nav class="container">
        <div class="navbar-collapse">
          <a href="/">
            <div class="navbar-brand">Submit</div>
          </a>
          <el-menu
            :default-active="activeIndex2"
            class="el-menu-demo"
            mode="horizontal"
            @select="handleSelect"
            text-color="#fff"
            active-text-color="#e74c3c"
            background-color="#3d4450"
          >
            <el-submenu index="1">
              <template slot="title">任务管理</template>
              <el-menu-item index="1-1">任务列表</el-menu-item>
              <el-menu-item index="1-2">新建任务</el-menu-item>
            </el-submenu>
            <el-submenu index="2">
              <template slot="title">提交人管理</template>
              <el-menu-item index="2-1">提交人名单</el-menu-item>
              <el-menu-item index="2-2">新建提交人</el-menu-item>
            </el-submenu>
            <el-submenu index="3">
              <template slot="title">标签管理</template>
              <el-menu-item index="3-1">新建标签</el-menu-item>
            </el-submenu>
            <el-submenu index="4">
              <template slot="title">关于submit</template>
              <el-menu-item index="4-1">组员简介</el-menu-item>
            </el-submenu>
          </el-menu>
        </div>
      </nav>
    </el-header>
    <el-main id="boder">
      <div class="container">
        <div class="row">
          <el-col id="mainstay" :span="8">
            <div class="ct">
              <div class="mn" style="margin-top: 20px; background: #fff">
                <div class="main-hnav" style="padding: 1px">
                  <h1 id="crtask" style="font-size: 24px">
                    任务详情
                    <h5 class="inline">
                      <a
                        id="task-name"
                        href=""
                        style="
                          font-size: 13px;
                          text-decoration: none;
                          color: #7c62ad;
                        "
                        @click.prevent="changeTitle"
                      >
                        - {{ this.task.title }}</a
                      >
                    </h5>
                    <span
                      id="task-status"
                      class="label"
                      style="font-size: 13px"
                      >{{ this.task.status }}</span
                    >
                    <a
                      id="task-period"
                      href=""
                      class="inline"
                      style="margin-left: 20px; font-size: 13px"
                      @click.prevent="changeDate"
                      >周期: {{ this.task.stime }}~{{ this.task.etime }}</a
                    >
                    <el-dialog
                      title="提示"
                      :visible.sync="dialogVisible"
                      width="30%"
                    >
                      <span> {{ this.message }}: </span>
                      <el-date-picker
                        type="datetime"
                        placeholder="选择日期"
                        format="yyyy-MM-dd HH:mm:ss"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        v-model="deadline"
                        :picker-options="endOptions"
                      >
                      </el-date-picker>
                      <span slot="footer" class="dialog-footer">
                        <el-button @click="dialogVisible = false"
                          >取 消</el-button
                        >
                        <el-button type="primary" @click="checkDate()"
                          >确 定</el-button
                        >
                      </span>
                    </el-dialog>

                    <span class="pull-right">
                      <span id="btn-outer"
                        ><a class="btn btn-success" @click.prevent="startTask"
                          ><i
                            :class="[
                              this.task.isStart === '运行'
                                ? 'el-icon-video-play'
                                : 'el-icon-video-pause',
                            ]"
                          ></i>
                          {{ this.task.isStart }}</a
                        ></span
                      >
                      <a class="btn btn-danger" @click.prevent="deleteTask"
                        >删除</a
                      >
                    </span>
                  </h1>
                </div>

                <el-divider></el-divider>
                <el-form ref="form" label-width="100px">
                  <div class="layer_cr">
                    <div class="mtw">
                      <div id="crinfo">
                        <el-row>
                          <el-col :span="24" class="description">
                            <span>任务描述: </span> <br />
                            {{ this.task.description }}
                          </el-col>
                        </el-row>

                        <el-row>
                          <el-col :span="24" class="commit-status">
                            <span>提交情况: </span> <br />
                            <div>
                              已提交人数: {{ this.task.sstatus }}
                              <a
                                href=""
                                class="pull-right"
                                @click.prevent="slist"
                                style="color: #7c62ad"
                                >查看详情</a
                              >
                            </div>
                            <div>
                              未提交人数: {{ this.task.dstatus }}
                              <a
                                href=""
                                class="pull-right"
                                @click.prevent="dlist"
                                style="color: #7c62ad"
                                >查看详情</a
                              >
                            </div>
                          </el-col>
                        </el-row>

                        <el-row>
                          <el-col :span="24" class="word">
                            <div>
                              <span> 文档详情: </span>
                              <el-upload
                                class="upload-demo pull-right"
                                ref="upload"
                                multiple
                                :limit="5"
                                action="http://localhost:8081/task/upload"
                                accept=".doc,.docx"
                                :on-preview="handlePreview"
                                :on-change="handleChange"
                                :on-remove="handleRemove"
                                :on-exceed="handleExceed"
                                :file-list="fileList"
                                :http-request="uploadFile"
                                :auto-upload="false"
                              >
                                <a slot="trigger" style="color: #7c62ad"
                                  >选取文件</a
                                >
                                <a
                                  style="color: #7c62ad"
                                  @click.prevent="submit"
                                  >点击上传</a
                                >
                              </el-upload>
                            </div>

                            <div
                              v-for="(item, index) in task.word"
                              :key="item"
                              :offset="index"
                            >
                              <a
                                href=""
                                @click.prevent="fileDownload(item.word)"
                                >{{ item.word }}</a
                              >
                            </div>
                          </el-col>
                        </el-row>

                        <el-row>
                          <el-col :span="24" class="sub-info">
                            <span>提交人员详情</span>
                            <a
                              href=""
                              type="text"
                              @click.prevent="visibleTable"
                              class="selectButton pull-right"
                              style="color: #7c62ad"
                              >查看详情</a
                            >
                            <el-dialog
                              title="提交人名单"
                              :visible.sync="dialogTableVisible"
                            >
                              <el-table
                                :data="tableData"
                                tooltip-effect="dark"
                                style="width: 100%"
                              >
                                <el-table-column
                                  type="selection"
                                  min-width="10%"
                                >
                                </el-table-column>
                                <el-table-column
                                  prop="id"
                                  label="id"
                                  min-width="35%"
                                >
                                </el-table-column>
                                <el-table-column
                                  prop="name"
                                  label="姓名"
                                  min-width="55%"
                                >
                                </el-table-column>
                              </el-table>
                              <div class="block" style="text-align: center">
                                <el-pagination
                                  @current-change="getPageData"
                                  :current-page="pageObject.pageNum"
                                  :page-size="pageObject.pageSize"
                                  layout="total, prev, pager, next, jumper"
                                  :total="pageObject.total"
                                >
                                </el-pagination>
                              </div>
                            </el-dialog>
                          </el-col>
                        </el-row>
                      </div>
                    </div>
                  </div>
                </el-form>
              </div>
            </div>
          </el-col>

          <el-col id="sidebar" :span="4">
            <aside id="task-login">
              <div class="panel task-panel">
                <div class="panel-heading">
                  <i class="el-icon-sort-up"></i>
                  欢迎!
                  <i class="el-icon-sort-down"></i>
                </div>
                <div class="login-panel text-center">
                  <img
                    src="https://cache.cswsadlab.com/wp-content/uploads/2015/03/笑颜1-150x150.jpg"
                    alt="头像"
                    class="avatar avatar-60 wp-user-avatar wp-user-avatar-60 photo avatar-default"
                    width="60"
                    height="60"
                  />
                  <a class="user-name" href="https://vcb-s.com/wp-admin/"
                    >kamike</a
                  >
                  <a
                    class="btn btn-inverse-primary"
                    href="#"
                    @click.prevent="logout"
                    title="退出登录"
                    >退出登录</a
                  >
                </div>
              </div>
            </aside>
          </el-col>
        </div>
      </div>
    </el-main>
    <el-footer id="task-footer">
      <div class="container">
        我们的目标是征服星河大海 Powered By
        <a href="/test" target="_blank">Submit</a>
      </div>
    </el-footer>
  </el-container>
</template>

<script>
import {
  getTask,
  changeDate,
  start,
  changeTaskName,
  pause,
} from "@/api/admin.js";
import { formatDate } from "@/utils/dateformat.js";
import { getToken } from "@/utils/auth";
import { isTaskFileExist } from "@/api/brand.js";
import { subTask } from "@/api/brand.js";
import { downTaskFile } from "@/api/file.js";
import { getListByTid } from "@/api/brand.js";
import { delTask, countSub, countDSub } from "@/api/brand.js";
import { uploadTask } from "@/api/file.js";
const pageParam = {
  list: [],
  pageNum: null,
  pageSize: null,
  total: null,
  totalPage: null,
};
export default {
  data() {
    return {
      activeIndex: "1",
      activeIndex2: "1",
      tid: null,
      cid: null,
      task: null,
      dialogVisible: false,
      dialogVisible1: false,
      deadline: "",
      message: "",
      warn: "",
      headers: {},
      fileList: [],
      dialogTableVisible: false,
      tableData: [],
      currentPage: 1,
      pageObject: null,
      fileData: {},
      endOptions: {
        disabledDate: (time) => {
          return time.getTime() <= new Date(this.task.stime).getTime();
        },
      },
    };
  },
  created() {
    this.headers.Authorization = getToken();
    this.cid = this.$route.query.cid;
    this.tid = this.$route.query.tid;
    this.pageObject = Object.assign({}, pageParam);
    this.getTaskInfo();
    this.getPageData(1);
  },
  methods: {
    getCount() {
      countSub(this.tid).then((response) => {
        this.task.sstatus = response.data;
      });
      countDSub(this.tid).then((response) => {
        this.task.dstatus = response.data;
      });
    },
    visibleTable() {
      this.dialogTableVisible = true;
    },
    logout() {
      this.$store.dispatch("LogOut").then((response) => {});
      this.$router.push({
        path: "/login",
      });
    },
    handleSelect(key, keyPath) {
      console.log(key, keyPath);
      if (keyPath[1] === "1-2") {
        this.$router.push({
          path: "/crtask",
          query: { cid: this.cid },
        });
      } else if (keyPath[1] === "1-1") {
        this.$router.push({
          path: "/coltask",
          query: { cid: this.cid },
        });
      } else if (keyPath[1] === "2-1") {
        this.$router.push({
          path: "/colsub",
          query: { cid: this.cid },
        });
      } else if (keyPath[1] === "2-2") {
        this.$router.push({
          path: "/crsub",
          query: { cid: this.cid },
        });
      } else if (keyPath[1] === "3-1") {
        this.$router.push({
          path: "/crtag",
          query: { cid: this.cid },
        });
      }
    },
    slist() {
      this.$router.push({
        path: "/slist",
        query: {
          cid: this.cid,
          tid: this.tid,
          taskName: this.task.title,
        },
      });
    },
    dlist() {
      this.$router.push({
        path: "/dlist",
        query: { cid: this.cid, tid: this.tid },
      });
    },

    checkDate() {
      changeDate(this.tid, this.deadline).then((response) => {
        this.getTaskInfo();
        this.dialogVisible = false;
      });
    },
    changeTitle() {
      this.$prompt("请输入新的任务标题", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(({ value }) => {
          if (!value) {
            this.$message({
              type: "warn",
              message: "任务名称不能为空",
            });
          } else if (value.length > 10) {
            this.$message({
              type: "warn",
              message: "任务名称不能超过10个字符",
            });
          } else {
            changeTaskName(this.tid, value).then((response) => {
              this.$message({
                type: "success",
                message: "已将任务标题修改为: " + value,
              });
              this.getTaskInfo();
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    fileDownload(filename) {
      var params = new URLSearchParams();
      params.append("filename", filename);
      downTaskFile(params).then((response) => {
        const blob = new Blob([response], { type: "text/pain;charset=utf-8" });
        const url = window.URL.createObjectURL(blob);
        const a = document.createElement("a");
        a.download = filename + ".docx";
        a.href = url;
        a.click();
        window.URL.revokeObjectURL(url);
      });
    },
    changeDate() {
      this.message = "请选择新的截至日期";
      this.dialogVisible = true;
    },
    startTask() {
      if (this.task.status === "已结束") {
        this.message = "该任务已结束，选择新的截至日期以开始任务   ";
        this.dialogVisible = true;
      } else if (this.task.status === "运行中") {
        pause(this.tid).then((response) => {
          this.task.isStart = "运行";
          this.getTaskInfo();
        });
      } else {
        start(this.tid, "").then((response) => {
          this.task.isStart = "暂停";
          this.getTaskInfo();
        });
      }
    },
    deleteTask() {
      this.$confirm("是否永久删除任务: " + this.task.title + "?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delTask(this.task.id).then((response) => {
          this.$router.push({
            path: "/coltask",
            query: { cid: this.cid },
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
      });
    },
    getTaskInfo() {
      getTask(this.tid).then((response) => {
        let data = response.data;
        this.task = {};
        this.task.id = data.id;
        this.task.description = data.description;
        this.task.word = data.words;
        console.log(this.task.word);
        if (this.task.word === "") this.task.word = "无相关文档";
        this.task.title = data.title;
        this.task.status = this.getTaskStatus(data.status);
        this.task.stime = data.createTime;
        this.task.etime = data.deadline;
        this.task.isStart = this.task.status === "运行中" ? "暂停" : "运行";
        this.getCount();
      });
    },
    getTaskStatus(status) {
      var name = "";
      switch (status) {
        case 0:
          name = "未开始";
          break;
        case 1:
          name = "运行中";
          break;
        case 2:
          name = "已结束";
          break;
        case 3:
          name = "暂停中";
          break;
      }
      return name;
    },

    uploadFile(file) {
      console.log(file.file);
      this.fileData.append("words", file.file);
    },

    submit() {
      if (this.fileList.length === 0) {
        this.$message({
          message: "请先选择文件",
          type: "warning",
        });
      } else {
        const isLt10M = this.fileList.every(
          (file) => file.size / 1024 / 1024 < 10
        );
        if (!isLt10M) {
          this.$message.error("请检查，上传文件大小不能超过10MB!");
        } else {
          this.fileData = new FormData();
          this.$refs.upload.submit();
          this.fileData.append("tid", this.tid);

          uploadTask(this.fileData).then((response) => {});
        }
      }
    },
    //移除
    handleRemove(file, fileList) {
      this.fileList = fileList;
      // return this.$confirm(`确定移除 ${ file.name }？`);
    },

    // 选取文件超过数量提示
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },

    //监控上传文件列表
    handleChange(file, fileList) {
      let existFile = fileList
        .slice(0, fileList.length - 1)
        .find((f) => f.name === file.name);
      if (existFile) {
        this.$message.error("当前文件已经存在!");
        fileList.pop();
      }
      this.fileList = fileList;
    },
    getPageData(val) {
      console.log(`当前页: ${val}`);
      this.tableData.splice(0, this.tableData.length);
      var params = new URLSearchParams();
      params.append("tid", this.tid);
      params.append("currentPage", val);
      params.append("pageSize", "");
      getListByTid(params).then((response) => {
        let data = response.data;
        this.pageObject.list = data.list;
        this.pageObject.pageNum = data.pageNum;
        this.pageObject.pageSize = data.pageSize;
        this.pageObject.total = data.total;
        this.pageObject.totalPage = data.totalPage;
        for (var i = 0; i < this.pageObject.list.length; i++) {
          var item = [];
          item.id = data.list[i].id;
          item.name = data.list[i].name;
          item.phone = data.list[i].phone;
          item.wechat = data.list[i].wechat;
          this.tableData.push(item);
        }
      });
    },
  },
};
</script>

<style scoped>
.description {
  color: #666666;
  background-color: #ecf0f1;
  border: 1px solid #ecf0f1;
  padding: 15px;
  margin-bottom: 20px;
  border: 1px solid transparent;
  border-radius: 4px;
  overflow: auto;
}
#task-footer {
  display: block;
  text-align: center;
  color: #ecf0f1;
  padding: 50px;
  background-color: #3d4450;
}
#task-footer a {
  color: #ecf0f1;
}

#task-footer {
  display: block;
  text-align: center;
  color: #ecf0f1;
  padding: 50px;
  background-color: #3d4450;
}
#task-footer a {
  color: #ecf0f1;
}

#sidebar aside > div {
  border-radius: 4px;
  box-shadow: 1px -1px 6px rgba(155, 89, 182, 0.5);
  -webkit-box-shadow: 1px -1px 6px rgba(155, 89, 182, 0.5);
  background-color: white;
}

.panel {
  margin-bottom: 20px;
  border: 1px solid transparent;
}

.panel-heading {
  border-bottom: none;
  font-size: 14px;
  line-height: 14px;
  font-weight: normal;
}
.panel-heading {
  padding: 10px 15px;
  border-bottom: 1px solid transparent;
  border-top-right-radius: 3px;
  border-top-left-radius: 3px;
}
.login-panel {
  height: 100px;
  padding: 20px 0px;
}
.text-center {
  text-align: center;
}
.login-panel img {
  margin-right: 20px;
  border-radius: 50%;
  border: 2px solid white;
  background-color: white;
  -webkit-transition: all 0.3s ease;
  -moz-transition: all 0.3s ease;
  -o-transition: all 0.3s ease;
  transition: all 0.3s ease;
}
img {
  max-width: 100%;
  border-radius: 4px;
  vertical-align: middle;
  height: auto;
}
.login-panel .user-name {
  margin-right: 20px;
}
.btn-inverse-primary {
  color: white !important;
  background-color: #3d4450;
  border-color: #3d4450;
  -webkit-transition: all 0.3s ease;
  -moz-transition: all 0.3s ease;
  -o-transition: all 0.3s ease;
  transition: all 0.3s ease;
}
.task-panel a {
  color: #666666;
}
.btn {
  display: inline-block;
  padding: 6px 12px;
  margin-bottom: 0;
  font-size: 12px;
  font-weight: normal;
  line-height: 1.428571429;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: 4px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -o-user-select: none;
  user-select: none;
}
.btn-danger {
  color: #ffffff;
  background-color: #e74c3c;
  border-color: #e74c3c;
  transition: all 0.25s ease-in-out;
  -webkit-transition: all 0.25s ease-in-out;
  -moz-transition: all 0.25s ease-in-out;
  -o-transition: all 0.25s ease-in-out;
}
.el-header {
  background-color: #3d4450;
  border-color: #080808;
  top: 0;
  z-index: 1030;
}
#header {
  display: fixed;
  padding: 0;
  min-height: 50px;
}

.container {
  max-width: 1170px;
  margin-right: auto;
  margin-left: auto;
}

.el-menu {
  background-color: #3d4450;
  font-size: 14px;
  display: block;
}

.el-menu::after,
.el-menu::before {
  display: none;
}

.navbar-brand {
  margin-left: -15px;
  width: 200px;
  height: 50px;
  display: block;
  float: left;
  font-size: 40px;
  color: white;
  text-align: center;
}

.navbar-collapse {
  margin-left: -15px;
  margin-right: -15px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1);
}

#boder {
  padding: 30px 0;
}

.container {
  padding-left: 30px;
  padding-right: 20px;
}

.row {
  margin-right: -15px;
  margin-left: -15px;
}

#mainstay {
  width: 66.666666666666%;
  float: left;
  padding-right: 15px;
  padding-left: 15px;
}

#sidebar {
  width: 33.333333333333%;
}
body {
  word-wrap: break-word;
}
body {
  font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;
  font-size: 12px;
  line-height: 1.428571429;
  color: #3d4450;
}
a {
  text-decoration: none;
}

.ct {
  border-radius: 4px;
  box-shadow: -1px -1px 10px rgba(155, 89, 182, 0.5);
  -webkit-box-shadow: -1px -1px 10px rgba(155, 89, 182, 0.5);
  position: relative;
  margin-bottom: 30px;
  padding-bottom: 20px;
  min-height: 20px;
  padding: 19px;
  padding-bottom: 19px;
  margin-bottom: 30px;
  border-radius: 6px;
  background-color: #ffffff;
  -webkit-box-shadow: 0px 0px 10px #9b59b6;
  -moz-box-shadow: 0px 0px 10px #9b59b6;
  box-shadow: 0px 0px 10px #9b59b6;
}

.crinfo-btn {
  text-align: center;
}

.commit-status {
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
  border: 1px solid;
  border-top-color: currentcolor;
  border-right-color: currentcolor;
  border-bottom-color: currentcolor;
  border-left-color: currentcolor;
  clear: both;
  margin: 15px 0;
  padding: 15px 20px 15px 30px;
  background: #d9edf7;
  border-color: #bce8f1;
  color: #3a87ad;
}

.word {
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
  border: 1px solid;
  border-top-color: currentcolor;
  border-right-color: currentcolor;
  border-bottom-color: currentcolor;
  border-left-color: currentcolor;
  clear: both;
  margin: 15px 0px;
  padding: 15px 20px 15px 30px;
  background: #d9edf7;
  border-color: #bce8f1;
  color: #3a87ad;
}

.sub-info {
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
  border: 1px solid;
  border-top-color: currentcolor;
  border-right-color: currentcolor;
  border-bottom-color: currentcolor;
  border-left-color: currentcolor;
  clear: both;
  margin: 15px 0px;
  padding: 15px 20px 15px 30px;
  background: #d9edf7;
  border-color: #bce8f1;
  color: #3a87ad;
}
</style>